﻿//==============================================================================
// File: RegionBasedTabMenuDockManager.cs
// Created: 2010-06-07
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Windows;
using WpfRcp.Controls;
using Microsoft.Practices.Composite.Regions;

namespace WpfRcp.Core.Common
{
    public class RegionBasedTabMenuDockManager : TabMenuDockManager
    {
        private readonly IRegionManager m_RegionManager;

        public EventHandler<ContentClosedEventArgs> ContentClosed;

        public RegionBasedTabMenuDockManager(IRegionManager regionManager)
        {
            m_RegionManager = regionManager;
        }

        protected override void OnContentDock(TabMenuItemWindow window, TabMenuItem item)
        {
            object target = GetDock(window);
            if (target is string)
            {
                m_RegionManager.Regions[(string) target].Add(item);
                m_RegionManager.Regions[(string) target].Activate(item);
                SetDock(item, target);
            }
        }

        protected override void OnContentUndock(TabMenuItemWindow window, TabMenuItem item)
        {
            SetDock(window, GetDock(item));
            m_RegionManager.Regions[(string) GetDock(item)].Remove(item);

            int index = GetSingletonIndex(window);
            if (index == 0)
                RegisterWindow(window);
        }

        protected override void OnContentClose(TabMenuItem item, object content)
        {
            m_RegionManager.Regions[(string)GetDock(item)].Remove(item);

            if (ContentClosed != null)
                ContentClosed(this, new ContentClosedEventArgs((FrameworkElement) content));
        }

        protected override void OnContentClose(TabMenuItemWindow window, object content)
        {
            if (ContentClosed != null)
                ContentClosed(this, new ContentClosedEventArgs((FrameworkElement) content));
        }
    }

    public class ContentClosedEventArgs : EventArgs
    {
        public ContentClosedEventArgs(FrameworkElement content)
        {
            Content = content;
        }

        public FrameworkElement Content { get; private set; }
    }
}